home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 080 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 080 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / TUC / TUC.c < prev    next >
C/C++ Source or Header  |  1988-08-14  |  13KB  |  498 lines

  1. /******************************************************************************
  2. **********                                 **********
  3. **********              THE ULTIMATE CLOCK                   **********
  4. **********                                 **********
  5. *******************************************************************************
  6. **********                                 **********
  7. **********       Designed by Magic Ceee, Sanative Secrets 1987       **********
  8. **********                                 **********
  9. **********       Based on a novel by Mike Meyers (MCLK). Thanx!      **********
  10. **********                                 **********
  11. *******************************************************************************
  12. **********                                 **********
  13. **********  This stuff is in the public domain (to add pleasure...)  **********
  14. **********                                 **********
  15. ******************************************************************************/
  16.  
  17. #include "libraries/dos.h"
  18. #include "libraries/dosextens.h"
  19. #include "exec/types.h"
  20. #include "exec/memory.h"
  21. #include "exec/io.h"
  22. #include "exec/devices.h"
  23. #include "devices/trackdisk.h"
  24. #include "functions.h"
  25. #include "stdio.h"
  26. #include "exec/tasks.h"
  27. #include "devices/timer.h"
  28. #include "intuition/intuition.h"
  29. #include "intuition/intuitionbase.h"
  30. #include "graphics/gfxbase.h"
  31. #include "libraries/diskfont.h"
  32.  
  33. #define NO_DISK        "NO DISK"
  34. #define NO_DRIVE    "NO DRIVE"
  35. #define PROTECTED    "-P"
  36. #define NO_SPACE    "FULL"
  37. #define FIRST_STARTUP    "- WAIT -"
  38.  
  39. #define HELP        "?"
  40. #define NO_COMMERCIAL    "-n"
  41. #define WAKE_ME_UP    "-a"
  42. #define NO_PLEASURE    "\n- No ultimate pleasure for you -\n\n"
  43. #define PLEASURE    "\n- Ultimate pleasure for you!!! -\n\n"
  44. #define BAD_TIME    "\nBad format! Use <TUC -a hh:mm:ss>\n\n"
  45. #define COMMERCIAL    "\nTUC V1.3, by Magic Ceee. Add '?' for help\n"
  46.  
  47. #define MAGICFLAX    (SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|WINDOWDEPTH)
  48. #define WAIT_TIME    250000
  49.  
  50. /*---------------------------------------------------------------------------*/
  51.  
  52.        struct IntuitionBase    *IntuitionBase;
  53.        struct GfxBase        *GfxBase;
  54.        struct Library        *DiskfontBase;
  55.        struct MsgPort        *diskport;
  56.        struct IOExtTD        *diskreq;
  57.        struct Lock        *lock,*mylock;
  58.        struct Window        *MagicWindow;  
  59.        struct RastPort        *MagicRast;
  60.        struct timerequest    Time_Req;
  61.        struct MsgPort        *Timer_Port;
  62.        struct DateStamp        MagicDate;
  63.        struct IntuiMessage    *MagicMsg;
  64.        struct Task        *FindTask();
  65. extern struct MsgPort         *CreatePort();
  66. extern struct IORequest     *CreateExtIO();
  67.  
  68. struct AlertMessage
  69.        {
  70.         WORD X;
  71.     BYTE Y;
  72.     char text[58];
  73.     BYTE count;
  74.        }
  75.     
  76. texttab[] = {
  77.         80,17,"       Don't panic, son. It's just TUC saying hello...   ",99,
  78.     80,32,"        - This space for rent - This space for rent -    ",0 };
  79.  
  80. /*---------------------------------------------------------------------------*/
  81.  
  82. BYTE AlertBuf[20],dfree1[10],dfree2[10],dfree3[10],MagicBuffer[100],
  83.      *led=0xbfe001,*drive[3] = { "DF0:","DF1:","DF2:" };
  84.  
  85. SHORT dev_error,no_drive=FALSE,disk_status,pleasure=FALSE,prot,alert=FALSE,
  86.       ultimate_fun=FALSE;
  87.  
  88. /*---------------------------------------------------------------------------*/
  89.  
  90. struct NewWindow nw = {
  91.     0,0,640,10,1,2,CLOSEWINDOW,MAGICFLAX,NULL,NULL,NULL,NULL,NULL,
  92.     0,0,0,0,WBENCHSCREEN };
  93.  
  94. struct IntuiText Date_Text = { 1,2,JAM2,0,0,NULL,MagicBuffer,NULL };
  95.  
  96. struct TextFont *Pleasure8;
  97.  
  98. struct TextAttr PleasureFont = { "d132.font",8,0,0 };
  99.  
  100.  
  101. /*****************************************************************************/
  102.  
  103. main(argc,argv)
  104. int argc;
  105. char *argv[];
  106. {
  107.  int hours,minutes,secs,i;
  108.  long chip_free,fast_free,total;
  109.  
  110.  if(!(IntuitionBase=(struct IntuitionBase *)
  111.    OpenLibrary("intuition.library",0)))
  112.      done(20,"Can't open Intuition library");
  113.  
  114.  if(!(GfxBase=(struct GfxBase *)
  115.    OpenLibrary("graphics.library",0)))
  116.      done(20,"Can't open Graphics library");
  117.  
  118.  if(!(DiskfontBase=(struct Library *)OpenLibrary("diskfont.library",0)))
  119.      done(20,"Can't open Diskfont library"); 
  120.  
  121.  i=strcmp(argv[1],NO_COMMERCIAL);
  122.  if(!i) goto mega_schmand;
  123.  
  124.  i=strcmp(argv[1],WAKE_ME_UP);
  125.  if(!i) 
  126.    {
  127.     alert=TRUE;
  128.     goto mega_schmand;
  129.    } 
  130.  
  131.  i=strcmp(argv[1],HELP);
  132.  if(!i)
  133.    {
  134.     Tell_The_Truth();
  135.     exit(0);
  136.    }
  137.  
  138.  Print(COMMERCIAL);
  139.  
  140. mega_schmand:
  141.  if((Timer_Port=CreatePort("Timer Port", 0))==NULL)
  142.    done(20, "Can't create timer port");
  143.  
  144.  if(OpenDevice(TIMERNAME,UNIT_VBLANK,&Time_Req,0)!= NULL)
  145.    done(20, "Can't open timer device") ;
  146.  
  147.  Time_Req.tr_node.io_Message.mn_ReplyPort=Timer_Port;
  148.  Time_Req.tr_node.io_Command=TR_ADDREQUEST;
  149.  Time_Req.tr_node.io_Flags=0;
  150.  Time_Req.tr_node.io_Error=0;
  151.  
  152.  if(!(Pleasure8=(struct TextFont *)OpenDiskFont(&PleasureFont)))
  153.    {
  154.     nw.Width=564;
  155.     nw.LeftEdge=27;
  156.     Print(NO_PLEASURE);
  157.    }
  158.  
  159.  else
  160.      {
  161.       pleasure=TRUE;
  162.       Print(PLEASURE);
  163.      }
  164.  
  165.  if(!(MagicWindow=OpenWindow(&nw)))
  166.    done(20,"Can't open window");
  167.     
  168.  if(pleasure)
  169.    {
  170.     SetFont(MagicWindow->RPort,Pleasure8);
  171.     strcpy(dfree1,FIRST_STARTUP);
  172.     strcpy(dfree2,FIRST_STARTUP);
  173.     strcpy(dfree3,FIRST_STARTUP);
  174.    }
  175.  
  176.  SetTaskPri(FindTask(0),20);
  177.  
  178.  if(alert==TRUE && strlen(argv[2])!=8) Print(BAD_TIME);
  179.  
  180.  for(;;)
  181.     {
  182.      DateStamp(&MagicDate);
  183.      chip_free=AvailMem(MEMF_CHIP);
  184.  
  185.      if(chip_free<100000 && ultimate_fun==FALSE)
  186.        {
  187.         DisplayAlert(RECOVERY_ALERT,&texttab,45);
  188.     ultimate_fun=TRUE;
  189.        }
  190.  
  191.      fast_free=AvailMem(MEMF_FAST);
  192.      total=fast_free+chip_free;
  193.      hours=MagicDate.ds_Minute/60;
  194.      minutes=MagicDate.ds_Minute%60;
  195.      secs=MagicDate.ds_Tick/TICKS_PER_SECOND;
  196.  
  197.      sprintf(AlertBuf,"%02d:%02d:%02d",hours,minutes,secs);
  198.  
  199.      if(pleasure)
  200.        {
  201.         if(secs==0 || secs==20 || secs==40) Add_The_Ultimate_Pleasure();
  202.  
  203.         sprintf(MagicBuffer," Chip: %6ld   Fast: %7ld   Total: %7ld   DF0: %8s   DF1: %8s   DF2: %8s   Time: %02d:%02d:%02d ",
  204.         chip_free,fast_free,total,dfree1,dfree2,dfree3,hours,minutes,secs);
  205.        }
  206.  
  207.      else sprintf(MagicBuffer," Chip: %6ld  Fast: %7ld  Total: %7ld  Time: %02d:%02d:%02d",
  208.             chip_free,fast_free,total,hours,minutes,secs);
  209.  
  210.      PrintIText(MagicWindow->RPort,&Date_Text,28,1);
  211.  
  212.      if(alert) 
  213.        {
  214.         i=strcmp(AlertBuf,argv[2]);
  215.     if(!i) WakeUp();
  216.        }    
  217.  
  218.      if(minutes==0 && secs==0)
  219.        {
  220.     for(i=0;i<3;i++)
  221.        {
  222.             *led|=2;
  223.         Delay(40);
  224.         *led&=253;
  225.         Delay(40);
  226.        }
  227.        }
  228.  
  229.      Time_Req.tr_time.tv_secs=0;
  230.      Time_Req.tr_time.tv_micro=WAIT_TIME;
  231.      SendIO(&Time_Req.tr_node);
  232.  
  233.      Wait(1<<MagicWindow->UserPort->mp_SigBit | 1<<Timer_Port->mp_SigBit);
  234.  
  235.      while(MagicMsg=GetMsg(MagicWindow->UserPort))
  236.       {
  237.        if(MagicMsg->Class==CLOSEWINDOW)
  238.          {
  239.           ReplyMsg(MagicMsg);
  240.           done(0,"exit");
  241.          }
  242.        ReplyMsg(MagicMsg);
  243.       }
  244.  
  245.      GetMsg(Timer_Port);
  246.     }
  247.  
  248. }
  249.  
  250. /*****************************************************************************/
  251.  
  252. done(how,why)
  253. int how;
  254. char *why;
  255. {
  256.  AbortIO(&Time_Req.tr_node);
  257.  if(MagicWindow) CloseWindow(MagicWindow);
  258.  if(Time_Req.tr_node.io_Message.mn_ReplyPort) CloseDevice(&Time_Req);
  259.  if(Timer_Port) DeletePort(Timer_Port);
  260.  if(IntuitionBase) CloseLibrary(IntuitionBase);
  261.  if(GfxBase) CloseLibrary(GfxBase);
  262.  if(DiskfontBase) CloseLibrary(DiskfontBase);
  263.  exit(how);
  264. }
  265.  
  266. /*****************************************************************************/
  267.  
  268. CheckDrive(which_drive,cmd)
  269. int which_drive;
  270. long cmd;
  271. {
  272.  diskport=CreatePort(0,0);
  273.  
  274.  if(diskport==0) return(0);
  275.  
  276.  diskreq=(struct IOExtTD *)CreateExtIO(diskport,sizeof(struct IOExtTD));
  277.  
  278.  if(diskreq==0) 
  279.    {
  280.     DeletePort(diskport); 
  281.     return(0); 
  282.    }
  283.  
  284.  dev_error=OpenDevice(TD_NAME,which_drive,diskreq,0);
  285.  
  286.  if(dev_error)
  287.    {
  288.     if(which_drive==0) sprintf(dfree1,NO_DRIVE);
  289.     if(which_drive==1) sprintf(dfree2,NO_DRIVE);
  290.     if(which_drive==2) sprintf(dfree3,NO_DRIVE);
  291.     no_drive=TRUE;
  292.     goto knarz;
  293.    }
  294.  
  295.  diskreq->iotd_Req.io_Command=cmd;
  296.  DoIO(diskreq);
  297.  disk_status=diskreq->iotd_Req.io_Actual;
  298.  
  299.  if(disk_status==255)
  300.    {
  301.     if(which_drive==0) sprintf(dfree1,NO_DISK);
  302.     if(which_drive==1) sprintf(dfree2,NO_DISK);
  303.     if(which_drive==2) sprintf(dfree3,NO_DISK);
  304.     no_drive=TRUE;
  305.    }
  306.  
  307.  CloseDevice(diskreq);
  308. knarz:
  309.  DeleteExtIO(diskreq, sizeof(struct IOExtTD));
  310.  DeletePort(diskport);
  311. }
  312.  
  313. /*****************************************************************************/
  314.  
  315. struct IORequest *CreateExtIO(ioReplyPort,size)
  316. struct MsgPort *ioReplyPort;
  317. LONG size;
  318. {
  319.  struct IORequest *ioReq;
  320.  
  321.  if(ioReplyPort==0) return((struct IORequest *)0);
  322.  
  323.  ioReq=(struct IORequest *)AllocMem(size,MEMF_CLEAR|MEMF_PUBLIC);
  324.  
  325.  if(ioReq==0) return((struct IORequest *)0);
  326.  
  327.  ioReq->io_Message.mn_Node.ln_Type=NT_MESSAGE;
  328.  ioReq->io_Message.mn_Node.ln_Pri=0;
  329.  ioReq->io_Message.mn_ReplyPort=ioReplyPort;
  330.  
  331.  return(ioReq);
  332. }
  333.   
  334. /*****************************************************************************/
  335.  
  336. DeleteExtIO(ioExt,size)
  337. struct IORequest *ioExt;
  338. LONG size;
  339. {
  340.  ioExt->io_Message.mn_Node.ln_Type=0xff;
  341.  ioExt->io_Device=(struct Device *)-1;
  342.  ioExt->io_Unit=(struct Unit *)-1;
  343.  
  344.  FreeMem (ioExt, size);
  345. }
  346.  
  347. /*****************************************************************************/
  348.  
  349. GetDFree(which)
  350. int which;
  351. {
  352.  long free;
  353.  
  354.  if(lock=Lock(drive[which],ACCESS_READ))
  355.        {
  356.         free=avail(lock);
  357.         UnLock(lock);
  358.        }   
  359.  
  360.  if(which==0 && free!=0) sprintf(dfree1,"%6ld",free);
  361.  else if(which==0 && free==0) sprintf(dfree1,"%s",NO_SPACE);
  362.  
  363.  if(which==1 && free!=0) sprintf(dfree2,"%6ld",free);
  364.  else if(which==1 && free==0) sprintf(dfree2,"%s",NO_SPACE);
  365.  
  366.  if(which==2 && free!=0) sprintf(dfree3,"%6ld",free);
  367.  else if(which==2 && free==0) sprintf(dfree3,"%s",NO_SPACE);
  368.  
  369.  if(prot)
  370.    {
  371.     if(which==0) strcat(dfree1,PROTECTED);
  372.     if(which==1) strcat(dfree2,PROTECTED);
  373.     if(which==2) strcat(dfree3,PROTECTED);
  374.    } 
  375. }
  376.  
  377. /*---------------------------------------------------------------------------*/
  378.  
  379. avail(disk)
  380. struct Lock **disk;
  381. {
  382.  struct InfoData *info;
  383.  
  384.  long bytes;
  385.  
  386.  info=AllocMem(sizeof(struct InfoData),MEMF_PUBLIC);
  387.  Info(disk,info);
  388.  bytes=(info->id_NumBlocks-info->id_NumBlocksUsed)*info->id_BytesPerBlock;
  389.  FreeMem(info,sizeof(struct InfoData));
  390.  return(bytes);
  391. }
  392.  
  393. /*****************************************************************************/
  394.  
  395. Add_The_Ultimate_Pleasure()
  396. {
  397.  int i;
  398.  
  399.  for(i=0;i<3;i++)
  400.     {
  401.      CheckDrive(i,TD_CHANGESTATE);
  402.  
  403.      if(no_drive==FALSE)
  404.        {
  405.     CheckProt(i);
  406.         GetDFree(i);
  407.        }
  408.  
  409.      else no_drive=FALSE;
  410.     }    
  411. }
  412.  
  413. /*****************************************************************************/
  414.  
  415. Tell_The_Truth()
  416. {
  417.  int i,j;
  418.  
  419.  MagicRast=IntuitionBase->FirstScreen->FirstWindow->RPort;
  420.  
  421.  Print(" \n\n\n");
  422.  Print("       THE ULTIMATE CLOCK - MADE FOR YOU TO ADD THE ULTIMATE PLEASURE\n");
  423.  Print("               - Designed by Magic Ceee, 18-Dec-87, 08:31:49 -\n\n");
  424.  Print("      To add ultimate pleasure,there has to be a 132-colums font named\n");
  425.  Print("      <d132.font>  in the  directory  currently  assigned to \"fonts:\".\n");
  426.  Print("      This beastie then will provide the ultimate memory report, which\n");
  427.  Print("      will be updated  every 20 seconds. If you don't have such a font\n");
  428.  Print("      handy, you'll face the  simple memory clock, which has  actually\n");
  429.  Print("      a lack of pleasure.\n");
  430.  Print("      To kick the \"Add '?' for help\" - prompt, add \"-n\" to the command\n");
  431.  Print("      line. To enable the alert funktion, type TUC -a hh:mm:ss to  get\n");
  432.  Print("      alarmed when your time has come. Have fun!\n\n");
  433.  Print("               - Designed by Magic Ceee, 18-Dec-87, 08:31:49 -\n");
  434.  Print("       THE ULTIMATE CLOCK - MADE FOR YOU TO ADD THE ULTIMATE PLEASURE\n");
  435.  
  436.  for(j=0;j<3;j++)
  437.     {
  438.      for(i=0;i<10;i++)
  439.      ScrollRaster(MagicRast,0,-1,2,10,600,180);
  440.  
  441.      for(i=0;i<10;i++)
  442.          ScrollRaster(MagicRast,0,1,2,10,600,180);
  443.     }
  444.  
  445.  bye:
  446.   CloseLibrary(IntuitionBase);
  447.   CloseLibrary(GfxBase);
  448. }
  449.  
  450. /*****************************************************************************/
  451.  
  452. WakeUp()
  453. {
  454.  int i;
  455.  
  456.  for(i=0;i<5;i++)
  457.     {
  458.      DisplayBeep(IntuitionBase->FirstScreen);
  459.      *led|=2;
  460.      Delay(40);
  461.      *led&=253;
  462.      Delay(40);
  463.     }
  464.  
  465. /*****************************************************************************/
  466.  
  467. CheckProt(which_drive)
  468. int which_drive;
  469. {
  470.  struct InfoData    *id;
  471.  struct lock        *templock;
  472.  int success;
  473.  
  474.  prot=FALSE;
  475.  id=(struct InfoData *)AllocMem(sizeof(struct InfoData),MEMF_CLEAR);
  476.  templock=Lock(drive[which_drive],ACCESS_READ);
  477.  
  478.  if(templock)
  479.    {
  480.     success=Info(templock,id);
  481.    
  482.     if(success)
  483.       if(id->id_DiskState==ID_WRITE_PROTECTED) prot=TRUE;
  484.     UnLock(templock);
  485.    }
  486.  
  487.  FreeMem(id,sizeof(struct InfoData));
  488. }
  489.    
  490. /*****************************************************************************/
  491.  
  492. Print(s)
  493. char *s;
  494. {
  495.  Write(Output(),s,strlen(s));
  496. }
  497.